package com.definesys.smecrailtransit.module.SiteDetails;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import com.bigkoo.pickerview.OptionsPickerView;
import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailConstant;
import com.definesys.smecrailtransit.databinding.FragmentSiteDetailsBinding;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleStation;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.hwangjr.rxbus.Bus;

import java.util.ArrayList;
import java.util.HashMap;

import static com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsFragment.REFRESH;

/**
 * Created by xupeizuo on 2017/8/23.
 */

public class SiteDetailsViewModel {

    private Context context;
    private FragmentSiteDetailsBinding viewDataBinding;
    private Bus mRxBus;
    private OptionsPickerView siteOptions;
    private HashMap<String,Object> hashMap=new HashMap<>();
    private ArrayList<String> lineBeen;
    private ArrayList<ArrayList<String>> arrayLists;
    private HashMap<String,ArrayList<EleStation>> map;
    private int stationsPosition=0;
    private String stationsName;

    public void setStationsName(String name) {
        this.stationsName = name;
    }

    public SiteDetailsViewModel(Context context, FragmentSiteDetailsBinding viewDataBinding, Bus mRxbus) {
        this.context = context;
        this.viewDataBinding = viewDataBinding;
        this.mRxBus = mRxbus;
    }

    public void siteClick(View view){
        switch (view.getId()){
            case R.id.change_site:
                if(siteOptions == null){
                    _showSites();
                }
                siteOptions.setSelectOptions(0, stationsPosition);//默认选中项
                siteOptions.show();
                break;
        }
    }


    private void _showSites(){

        siteOptions = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String line=lineBeen.get(options1);
                String stations=arrayLists.get(options1).get(options2);
                if(stations != null && !stations.equals("")){
                    viewDataBinding.siteSwitched.setText(stations);
                    ArrayList<EleStation> list=map.get(line);
                    if(CommonUtils.notEmpty(list)){
                        for(int i =0;i<list.size();i++){
                            EleStation eleStation=list.get(i);
                            if(eleStation.getStationName().equals(stations)){
                                hashMap.put("stationId",eleStation.getStationId());
                                hashMap.put("stationName",eleStation.getStationName());
                                stationsPosition=i;
                                viewDataBinding.getRoot().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        mRxBus.post(REFRESH,hashMap);
                                        viewDataBinding.invalidateAll();
                                    }
                                },200);
                            }
                        }
                    }
                }
            }
        })
                .setTitleText("站点选择")
                .setContentTextSize(25)//设置滚轮文字大小
                .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                .setBgColor(Color.BLACK)
                .setTitleBgColor(Color.DKGRAY)
                .setTitleColor(Color.LTGRAY)
                .setCancelColor(Color.WHITE)
                .setSubmitColor(Color.WHITE)
                .setTextColorCenter(Color.LTGRAY)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setBackgroundId(0x66000000) //设置外部遮罩颜色
                .build();

        siteOptions.setPicker(lineBeen, arrayLists);//二级选择器
    }

    public void _initTitle(){
        lineBeen= SmecRailConstant.getLineBeen();
        map=SmecRailConstant.getMap();
        arrayLists=SmecRailConstant.getArrayLists();
    }

}
